<template>
    <div class="processMsgBox">
        <modal
            v-model="value"
            ref="msgModal"
            :label-width="80"
            :closable="false"
            :mask-closable='false'
            :width="modalWidth"
            :styles="{top: '180px'}">
            <div slot="header" style="text-align: center;font-size: 24px;font-weight: bolder;">
                {{messageTitle}}
            </div>
            <Spin fix size="large" v-if="loading">
                <slot name="loading"></slot>
            </Spin>
            <Form ref="messageForm" :model="formData" inline :label-width="labelWidth">
                <Form-item label="留言类型" prop="remindType" style="width: 550px;" >
                    <Select v-model="formData.remindType" @on-change="changeMsgType">
                        <Option v-for="item in typeData" :value="item.value">{{item.title}}</Option>
                    </Select>
                </Form-item>
                <Form-item label="留言原因" prop="remindReason" style="width: 550px;" v-show="reasonVisible">
                    <Select v-model="formData.remindReason"
                            multiple
                            :label-in-value="true"
                            clearable
                            @on-clear="clearReason"
                            @on-change="changeReason"
                    >
                        <Option v-for="item in currentReasonDataList" :value="item.value">{{item.title}}</Option>
                    </Select>
                </Form-item>
                <Form-item label="留言" prop="remindContent">
                    <textarea v-model="formData.remindContent" type="textarea" :rows="4" style="width: 400px;height: 100px;line-height: 20px"
                              placeholder="请输入留言" maxlength="512"/>
                </Form-item>
                <div v-if="approveRecord.stageId != '' && isCheck" style="height: 50px;width: 83%;text-align: right">
                    <Button @click="reject"
                            type="warning" ghost>退回
                    </Button>
                    <Button @click="pause" style="margin-left: 10px"
                           type="error" ghost>暂停案件
                    </Button>
                </div>
                <div v-if="approveRecord.stageId != '' && isTrue" style="height: 50px;width: 83%;text-align: right;padding-right: 30px">
                    <Button @click="rejectAsset"
                            type="warning" ghost>退回
                    </Button>
                </div>
            </Form>
            <div slot="footer">
                <Button type="primary" @click="save">确定</Button>
                <Button type="text" @click="cancel">关闭</Button>
            </div>
        </modal>
    </div>
</template>

<script>
    import {getDictDataByType} from "../../../../../basic/api/admin/datadic";
    import {createRemind} from "@/projects/afs-core-business/api/afs-case/remind/remind";
    import {saveRemind} from "../../../../api/afs-case/approve/changeAssetsTask";


    export default {
        name: "remind",
        props: {
            value: {
                type: Boolean,
                default: false
            },
            messageTitle: {
                type: String,
                default: '添加留言'
            },
            modalWidth: {
                type: Number,
                default: 700
            },
            labelWidth: {
                type: Number,
                default: 150
            },
            approveRecord: {
                type: Object,
                default: () => {
                    return {
                        applyNo: '',
                        stageId: '',
                        approveRemark: '',
                        businessStateIn: '',
                        useScene:''
                    };
                }
            },
            isCheck: {
                type: Boolean,
                default: false
            },
            isTrue: {
                type: Boolean,
                default: false
            },
            isDealerCallBack:false
        },
        data() {
            return {
                formData: {
                    remindType: '',
                    remindTypeValue: '',
                    remindReason: [],
                    remindReasonValue: [],
                    remindContent: '',
                    applyNo: ''
                },
                loading: false,
                currentReasonDataList: [],
                typeData: [],
                remindConst:{
                    inner:{
                        val:"reasonInner",
                        title:"内部留言"
                    },
                    reject:{
                        val:"reasonReject",
                        title:"内部件退回"
                    },
                    add:{
                        val:"reasonAdd",
                        title:"过程中补件"
                    },
                    undone:{
                        val:"reasonUndone",
                        title:"电核未完成"
                    }
                },
                reasonVisible:true
            }
        },
        methods: {
            cancel() {
                this.$emit('input', false);
                this.$emit('cancel');
            },
            save() {
                if (!(this.formData.remindType == this.remindConst.inner.val)) {
                    if (this.formData.remindReason == "") {
                        this.$Message.warning("请选择原因！")
                        return;
                    }
                    if(this.formData.remindContent.trim() == ""){
                        this.$Message.warning("请书写留言内容");
                        return;
                    }
                }
                    this.doSubmit('save');
            },
            reject() {
                if (!(this.formData.remindType == this.remindConst.inner.val)) {
                    if (this.formData.remindReason == "") {
                        this.$Message.warning("请选择原因！")
                        return;
                    }
                    if(this.formData.remindContent.trim() == ""){
                        this.$Message.warning("请书写留言内容");
                        return;
                    }
                }
                this.doSubmit('reject');
            },
            rejectAsset() {
                if (!(this.formData.remindType == this.remindConst.inner.val)){
                    if (this.formData.remindReason == "") {
                        this.$Message.warning("请选择原因！")
                        return;
                    }
                    if(this.formData.remindContent.trim() == ""){
                        this.$Message.warning("请书写留言内容");
                        return;
                    }
                }
                this.assetSubmit('reject');
            },
            pause() {
                this.doSubmit('parse');
            },
            doSubmit(operationType) {
                if (this.formData.remindType.trim()) {
                    if (operationType == "reject" || operationType == "parse") {
                        this.$Modal.confirm({
                            title: (operationType == "reject" ? "退回" : "暂停"),
                            content: "确认" + (operationType == "reject" ? "退回" : "暂停") + "当前案件?",
                            onOk: () => {
                                this.submit(operationType);
                            }
                        });
                    }else{
                        this.submit(operationType);
                    }
                } else {
                    this.$Message.warning("请选择留言类型！");
                }
            },
            assetSubmit(operationType){
                if (this.formData.remindType.trim()){
                        this.$Modal.confirm({
                            title: ("退回"),
                            content: "确认退回当前案件?",
                            onOk: () => {
                                this.submitAsset(operationType);
                            }
                        });
                }else {
                    this.$Message.warning("请选择留言类型！");
                }
            },
            submitAsset(operationType){
                this.formData.applyNo = this.approveRecord.applyNo;
                this.loading = true;
                this.$emit('beforeSubmit', operationType, this.approveRecord, this.formData);
                saveRemind({
                    remind: this.formData,
                    record: this.approveRecord,
                    remindOprType: operationType
                }).then(res => {
                    if (res.code == '0000') {
                        this.$emit('afterSubmit', operationType, this.approveRecord, this.formData);
                        this.$refs.messageForm.resetFields();
                        this.$Message.info("操作成功!")
                        this.$emit('input', false);
                        window.close();
                    }
                }).finally(() => {
                    this.loading = false;
                });
            },
            submit(operationType){
                this.formData.applyNo = this.approveRecord.applyNo;
                this.loading = true;
                this.$emit('beforeSubmit', operationType, this.approveRecord, this.formData);
                createRemind({
                    remind: this.formData,
                    record: this.approveRecord,
                    remindOprType: operationType
                }).then(res => {
                    if (res.code == '0000') {
                        this.$emit('afterSubmit', operationType, this.approveRecord, this.formData);
                        this.$Message.info("操作成功!")
                        this.$refs.messageForm.resetFields();
                        this.$emit('input', false);
                    }
                }).finally(() => {
                    this.loading = false;
                });
            },
            changeMsgType() {
                this.formData.remindContent = "";
                if (this.formData.remindType) {
                    this.formData.remindReason = [];
                    this.formData.remindReasonValue = [];
                    this.formData.remindTypeValue = '';
                    this.currentReasonDataList = [];
                    if(this.formData.remindType == this.remindConst.inner.val){
                        this.reasonVisible = false;
                    }else {
                        this.reasonVisible = true;
                        getDictDataByType(this.formData.remindType).then(res => {
                            if (res.code === "0000") {
                                this.currentReasonDataList = res.data;
                            }
                        });
                    }
                }
            },
            initMsgTypeData() {
                let param = "msgType";
                getDictDataByType(param).then(res => {
                    if (res.code === "0000") {
                        this.typeData = res.data;
                    }
                });
            },
            initRemindContent(){
                if(this.isDealerCallBack){
                    this.formData.remindContent="【经销商要求撤回】";
                }
            },
            clearReason(){
                this.formData.remindContent = "";
            },
            changeReason(reason){
                this.formData.remindContent = "";
                if(reason && reason.length > 0){
                    reason.forEach(rea=>{
                        this.formData.remindContent+=(rea.label+"；")
                    })
                }
            }
        },
        mounted() {
            this.initMsgTypeData();
            this.initRemindContent();
        }
    }
</script>

<style scoped>

</style>
